package com.own.business.comment.business.service.impl;

import com.own.business.comment.business.service.CommentService;
import com.own.business.comment.common.constant.ConstantComment;
import com.own.business.comment.entity.bo.CommentBo;
import com.own.business.comment.entity.map.CommentMapVo;
import com.own.business.comment.entity.po.Comment;
import com.own.business.comment.entity.query.CommentQuery;
import com.own.business.comment.entity.vo.CommentVo;
import com.own.business.comment.mapper.CommentMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * CommentServiceImpl
 *
 * @author chenxueli
 * @date 2023-07-03 14:15:52
 */
@Slf4j
@Service
public class CommentServiceImpl extends AbstractBaseService<
        Comment,
        CommentBo,
        CommentVo,
        CommentMapVo,
        CommentQuery,
        CommentMapper
        > implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     * 根据目标id查询评论
     *
     * @param targetId 目标id
     * @param query    查询条件
     * @return 评论列表
     */
    @Override
    public List<Comment> listByTargetId(Long targetId, CommentQuery query) {
        // 查询顶级评论信息
        var wrapper = lambdaQueryWrapper()
                .eq(Comment::getFkTargetId, targetId)
                .eq(Comment::getLevel, ConstantComment.TOP_LEVEL)
                .orderByDesc(Comment::getGmtCreate);


        return null;
    }

    /**
     * 保存评论
     *
     * @param userId   用户id
     * @param targetId 目标id
     * @param parentId 父级id
     * @param content  内容
     */
    @Override
    public void save(Long userId, Long targetId, Long parentId, String content) {
        var parent = parentId == null ? null : getPoById(parentId, false);
        var comment = new Comment();
        comment.setFkUserId(userId);
        comment.setFkTargetId(targetId);
        comment.setFkCommentId(parentId);
        comment.setContent(content);
        if (parent == null) {
            comment.setRootCommentId(null);
            comment.setLevel(ConstantComment.TOP_LEVEL);
        } else {
            comment.setRootCommentId(parent.getRootCommentId() == null ? parent.getId() : parent.getRootCommentId());
            comment.setLevel(parent.getLevel() + 1);
        }
        commentMapper.insert(comment);
    }

    /**
     * 根据用户id和评论id删除评论
     *
     * @param userId    用户id
     * @param commentId 评论id
     */
    @Override
    public void deleteByUserId(Long userId, Long commentId) {
        var wrapper = lambdaQueryWrapper()
                .eq(Comment::getFkUserId, userId)
                .eq(Comment::getId, commentId);
        commentMapper.delete(wrapper);
    }

    /**
     * 根据目标id删除评论
     *
     * @param targetId 目标id
     */
    @Override
    public void deleteByTargetId(Long targetId) {
        var wrapper = lambdaQueryWrapper().eq(Comment::getFkTargetId, targetId);
        commentMapper.delete(wrapper);
    }

}
